package com.situ.view;

import java.io.Serializable;
import java.util.Scanner;

import com.situ.domain.Major;
import com.situ.domain.Student;
import com.situ.service.StudentListService;

public class StudentView implements Serializable{
    /** serialVersionUID*/  
    private static final long serialVersionUID = 11651351L;
    
    StudentListService studentListService = new StudentListService();

    // 主界面，返回功能选项
    public char Menu() {
        System.out.println("****************学生档案管理系统****************");
        System.out.println("\t1.查看所有学生\t\t2.添加新生\n" + "\t3.按姓名查找\t\t4.按专业查找\n\t0.退出系统" + "\t\t请选择输入(0-4):");
        return TSUtility.readMenuSelection();
    }

    // 打印所有学生信息
    public void getAllStudents() {
        System.out.println("****************学生档案信息表*****************");
        for (Student s : studentListService.listStudents()) {
            System.out.println(s);
        }
        System.out.println();
    }

    // 添加新生
    public void addStudent() {
        Scanner scanner = new Scanner(System.in);
        Student student = new Student();
        Major major = new Major();
        // 获取新生信息
        System.out.println("*****************添加新生档案*****************");
        System.out.println();
        System.out.println("请输入新生ID(int):");
        if (scanner.hasNextInt()) {
            student.setId(scanner.nextInt());
        } else {
            System.out.println("请输入数字");
            return;
        }
        scanner.nextLine();
        System.out.println("请输入新生姓名(String):");
        student.setName(scanner.nextLine());
        System.out.println("请输入新生年龄(int):");
        student.setAge(scanner.nextInt());
        System.out.println("请输入新生性别(男1，女0 int):");
        student.setSex(scanner.nextInt());
        System.out.println("请输入新生专业ID(int):");
        major.setMajorId(scanner.nextInt());
        scanner.nextLine();
        System.out.println("请输入新生专业(String):");
        major.setMajorName(scanner.nextLine());
        // 执行插入操作
        student.setMajor(major);
        if (studentListService.insertStu(student)) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }

    }

    // 通过姓名查找学生，并打印该学生信息
    public void findStudentByName() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("*****************通过姓名查找学生*****************");
        System.out.println("请输入姓名：");
        String name = "";
        boolean flag = false;
        name = scanner.nextLine();
        for (Student student : studentListService.listStudents()) {
            if (name.equals(student.getName())) {
                System.out.println(student + "\n");
                flag = true;
            }
        }
        // 未查到
        if (!flag) {
            System.out.println("没有姓名为" + name + "同学的信息\n");
        }
    }

    // 查找某一专业的学生
    public void findStudentByMajor() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("*****************通过专业查找学生*****************");
        System.out.println("请输入专业：");
        String major = "";
        boolean flag = false;
        major = scanner.nextLine();
        for (Student student : studentListService.listStudents()) {
            if (major.equals(student.getMajor().getMajorName())) {
                System.out.println(student + "\n");
                flag = true;
            }
        }
        // 未查到
        if (!flag) {
            System.out.println("没有专业为" + major + "同学的信息\n");
        }
    }

}
